// Copyright 2011 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


/**
 * @fileoverview Creates and exposes a LayerManager and sets up piping of the
 * log to the console.
 *
 * @author jasonstredwick@google.com (Jason Stredwick)
 */


/**
 * The global console object.
 * @type { !Object}
 */
var console = typeof(console) == 'undefined' ? {} : console;


/**
 * A function that does nothing.
 * @param {string} data The input.
 */
function doNothing(data) {}


/**
 * The console log function.
 * @type {function(string)}
 */
console.log = console.log || doNothing;


/**
 * The console error function.
 * @type {function(string)}
 */
console.error = console.error || console.log;


/**
 * Fired when the layer manager's log has a new entry added.
 * @param {!bite.common.log.Simple.Entry} entry The entry that was logged.
 */
var onLogEntry = function(entry) {
  var msg = '[' + entry.timestamp + '] ' + entry.value;
  if (/^ERROR/.test(entry.value)) {
    console.error(msg);
  } else {
    console.log(msg);
  }
};


// Install the layer manager module.
var moduleName = 'shellExtension.LayerManager';
if (typeof(moduleObjs) == 'undefined' || !moduleObjs) {
  throw 'Modules expected; moduleObjs not found.';
} else if (typeof(moduleObjsOrder) == 'undefined' || !moduleObjsOrder) {
  throw 'Modules expected; moduleObjsOrder not found.';
} else if (!(moduleName in moduleObjs)) {
  throw 'shellExtension module is not loaded.';
} else {
  // TODO (jasonstredwick): Consider how best to load modules, identify newly
  // loaded modules, and so forth.
  moduleObjs[moduleName].install();
}


// Create a new layer manager instance and a getInstance function for
// retrieving it.
if (typeof(shellExtension) == 'undefined') {
  throw 'shellExtension is not defined. Can not create a new LayerManager.';
} else if (!shellExtension.LayerManager) {
  throw 'shellExtension.LayerManager is not defined.';
} else if (typeof(shellExtension.LayerManager) != 'function') {
  throw 'shellExtension.LayerManager is not a function.';
} else {
  /**
   * A function to retrieve the new LayerManager instance.
   * @type {function(): shellExtension.LayerManager}
   */
  shellExtension.getInstance = (function() {
    /**
     * Create a new layer manager.
     * @type {!shellExtension.LayerManager}
     */
    var manager = new shellExtension.LayerManager();
    manager.onLogUpdated.addListener(onLogEntry);

    return function() { return manager; };
  })();
}

